package achievement.dao.Impl;

import achievement.dao.SzlStudentArcDao;
import achievement.domain.SzlStudentArc;
import cn.hutool.core.util.StrUtil;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class SzlStudentArcDaoImpl implements SzlStudentArcDao {
    private Connection con;
    private PreparedStatement psmt;
    private ResultSet rs;
    @Override
    public void insert(SzlStudentArc szlStudentArc) {
        String sql = "insert into szl_studentArc\n" +
                "        (student_id,arc_id,type) \n" +
                "values(?,?,?)";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,szlStudentArc.getStudentId());
            psmt.setLong(2,szlStudentArc.getArcId());
            psmt.setInt(3,szlStudentArc.getType());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void updateById(SzlStudentArc szlStudentArc) {
        String sql = "update szl_studentArc set";
        List<Object> param = new ArrayList<>();
        if (szlStudentArc == null){
            return;
        }else{
            if(szlStudentArc.getId() != null){
                sql += " id = ?,";
                param.add(szlStudentArc.getId());
            }
            if(szlStudentArc.getStudentId() != null){
                sql += " student_id = ?,";
                param.add(szlStudentArc.getStudentId());
            }
            if(szlStudentArc.getArcId() != null){
                sql += " arc_id = ?,";
                param.add(szlStudentArc.getArcId());
            }
            if(szlStudentArc.getType() != null){
                sql += " type = ?,";
                param.add(szlStudentArc.getType());
            }
        }
        sql = StrUtil.strip(sql,null,",");
        sql+=" where id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            psmt.setLong(param.size()+1,szlStudentArc.getId());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void deleteById(Long id) {
        String sql = "delete from szl_studentArc where id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,id);
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public SzlStudentArc selectById(Long id) {
        String sql = "select * from szl_studentArc where id = ?";
        SzlStudentArc szlStudentArc = new SzlStudentArc();
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,id);
            rs = psmt.executeQuery();
            while (rs.next()){
                szlStudentArc.setId(rs.getLong(1));
                szlStudentArc.setStudentId(rs.getLong(2));
                szlStudentArc.setArcId(rs.getLong(3));
                szlStudentArc.setType(rs.getInt(4));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return szlStudentArc;
    }

    @Override
    public List<SzlStudentArc> selectList(SzlStudentArc szlStudentArc) {
        String sql = "select * from szl_studentArc where 1=1";
        List<SzlStudentArc> list = new ArrayList<>();
        List<Object> param = new ArrayList<>();
        if (szlStudentArc == null){
            sql = "select * from szl_studentArc";
        }else{
            if(szlStudentArc.getId() != null){
                sql += " and id = ?";
                param.add(szlStudentArc.getId());
            }
            if(szlStudentArc.getStudentId() != null){
                sql += " and student_id = ?";
                param.add(szlStudentArc.getStudentId());
            }
            if(szlStudentArc.getArcId() != null){
                sql += " and arc_id = ?";
                param.add(szlStudentArc.getArcId());
            }
            if(szlStudentArc.getType() != null){
                sql += " and type = ?";
                param.add(szlStudentArc.getType());
            }
        }
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            rs = psmt.executeQuery();
            while (rs.next()){
                SzlStudentArc szlStudentArc1 = new SzlStudentArc();
                szlStudentArc1.setId(rs.getLong(1));
                szlStudentArc1.setStudentId(rs.getLong(2));
                szlStudentArc1.setArcId(rs.getLong(3));
                szlStudentArc1.setType(rs.getInt(4));
                list.add(szlStudentArc1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return list ;
    }
}
